package com.joinval.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joinval.common.utils.PageUtils;
import com.joinval.common.utils.Query;
import com.joinval.common.utils.Result;
import com.joinval.common.utils.ShiroUtils;
import com.joinval.common.validator.AssertUtils;
import com.joinval.modules.sys.dao.SysUserAuditLogDao;
import com.joinval.modules.sys.entity.SysUserAuditLog;
import com.joinval.modules.sys.entity.SysUserDetail;
import com.joinval.modules.sys.entity.SysUserEntity;
import com.joinval.modules.sys.enums.AuditStatusEnums;
import com.joinval.modules.sys.enums.UserTypeEnums;
import com.joinval.modules.sys.form.SysUserAuditLogForm;
import com.joinval.modules.sys.service.SysUserAuditLogService;
import com.joinval.modules.sys.service.SysUserDetailService;
import com.joinval.modules.sys.service.SysUserService;
import com.joinval.modules.sys.vo.AuditVolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("sysUserAuditLogService")
public class SysUserAuditLogServiceImpl extends ServiceImpl<SysUserAuditLogDao, SysUserAuditLog> implements SysUserAuditLogService {


    @Autowired
    private SysUserDetailService sysUserDetailService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysUserAuditLog> page = this.page(
                new Query<SysUserAuditLog>().getPage(params),
                new QueryWrapper<SysUserAuditLog>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public Result audit(UserTypeEnums userType,SysUserAuditLogForm form) {
        if(userType == null){
            return Result.error("请选择审批用户类型");
        }
        if(form.getUserId() == null) {
            return Result.error("id不能为null");
        }
        // 新增审核记录到审核日志
        AuditStatusEnums auditStatus = AuditStatusEnums.getEnum(form.getAuditStatus());
        AssertUtils.notNull(auditStatus,"错误的审核状态！");
        //修改用户详情表中的状态
        SysUserEntity sysUserEntity = sysUserService.getOne(new QueryWrapper<SysUserEntity>().lambda().eq(SysUserEntity::getUserId, form.getUserId()));
        SysUserDetail sysUserDetail = sysUserDetailService.getOne(new QueryWrapper<SysUserDetail>().lambda().eq(SysUserDetail::getUserId, form.getUserId()));

        if(UserTypeEnums.VOL_USER.equals(userType)){
            if(!AuditStatusEnums.TO_AUDIT.value().equals(sysUserDetail.getVolAuditStatus())){
                throw new RuntimeException("当前用户未处待审核状态！无法审核");
            }
            sysUserDetail.setVolAuditStatus(form.getAuditStatus());
        }else if(UserTypeEnums.ORDER_USER.equals(userType)){
            if(!AuditStatusEnums.TO_AUDIT.value().equals(sysUserDetail.getOrgAuditStatus())){
                throw new RuntimeException("当前用户未处待审核状态！无法审核");
            }
            sysUserDetail.setOrgAuditStatus(form.getAuditStatus());
        }
        if(AuditStatusEnums.PASSED.equals(auditStatus)){
            sysUserService.addUserType(sysUserEntity,userType);
        }
        sysUserDetailService.updateById(sysUserDetail);
        SysUserAuditLog sysUserAuditLog = new SysUserAuditLog();
        sysUserAuditLog.setUserType(userType.value()).setAuditStatus(form.getAuditStatus()).setAuditMsg(form.getAuditMsg()).setUserId(form.getUserId()).setAuditBy(ShiroUtils.getUserId());
        baseMapper.insert(sysUserAuditLog);
        return Result.ok();
    }


    @Override
    public SysUserAuditLog getLastAuditLog(Long userId,UserTypeEnums userType) {
        return baseMapper.getLastAuditLog(userId,userType == null?null:userType.value());
    }


}
